package com.example.demo.util;

import sun.misc.BASE64Encoder;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {

	private MD5() {}
	
	public static String encode(String encode, String salt) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			String combine = combine(encode, salt);
			byte[] original = combine.getBytes();
			byte[] security = md.digest(original);
			return new BASE64Encoder().encode(security);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private static String combine(String encode, String salt) {
		StringBuilder sb = new StringBuilder();
		sb.append(salt.substring(0, 8));
		sb.append(encode);
		sb.append(salt.substring(8));
		return sb.toString();
	}

}
